* Replication
* John Gerring, Carl Henrik Knutsen, Matthew Maguire, Svend-Erik Skaaning, Jan Teorell, and Michael Coppedge. 2021. “Democracy and Human Development: Issues of Conceptualization and Measurement.” Democratization 28(2): 308-332.

* Table 2: MPI and mortality

* Model 8

* set maxvar
clear
clear matrix
clear mata
set maxvar 20000

* set directory

* load data
use "2021_Democratization_replication_data_posteriors.dta", clear
drop country_name country_text_id country v2x_polyarchy-durable_ln
order country_id year

set matsize 10000

* Collapse by decade using 10-year averages
xtset country_id year
gen decade = 10 * floor(year/10)
foreach var of varlist mpi_v901_stock_10-mpi_med_stock_10 {
qui egen mean_`var' = mean(`var'), by(country_id decade)
qui replace `var' = mean_`var'
qui drop mean_`var'
}
*end
drop year
duplicates drop
gen trend = .
replace trend = 1 if decade == 1900
replace trend = 2 if decade == 1910
replace trend = 3 if decade == 1920
replace trend = 4 if decade == 1930
replace trend = 5 if decade == 1940
replace trend = 6 if decade == 1950
replace trend = 7 if decade == 1960
replace trend = 8 if decade == 1970
replace trend = 9 if decade == 1980
replace trend = 10 if decade == 1990
replace trend = 11 if decade == 2000
replace trend = 12 if decade == 2010
rename decade year
* order
order country_id year trend
* save data
saveold "2021_Democratization_replication_data_posteriors_for_MI_10yr.dta", version(12) replace

* load MI data
use "2021_Democratization_replication_data_10yr_MI.dta", clear
qui mi import flong, m(imp) id(year country_id trend) imp(e_migdppc infmor_gapminder infmor_clio)
drop edi_mult_stock_10
qui mi stset, clear
qui mi xtset country_id trend
qui gen e_migdppc_ln = ln(e_migdppc)
qui gen infmor_gapminder_ln = ln(infmor_gapminder)
mi merge 1:1 country_id year using "2021_Democratization_replication_data_posteriors_for_MI_10yr.dta"

* put democracy index on 0-1 scale
foreach x of varlist mpi_med_stock_10 {
quietly mi xeq: sum `x'
quietly mi xeq: replace `x' = ((`x' - `r(min)') * (1 - 0)) / (`r(max)' - `r(min)')
}
*
qui mi stset, clear
qui mi xtset country_id trend

* Run baseline model
set more off
*qui mibeta F.infmor_gapminder_ln mpi_med_stock_10 e_migdppc_ln i.year i.country_id, vce(cluster country_id)
*qui local rsquared=e(r2_mi)
qui mi estimate, post: xtreg F.infmor_gapminder_ln mpi_med_stock_10 e_migdppc_ln i.year, fe vce(cluster country_id)
qui outreg2 using Table_2_Model_8.xls, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle(" ") addstat("Years", e(g_max), "Countries", e(N_g), "Observations", e(N)) dta replace


***Monte Carlo Estimates Using UDS 3202 Draw Posterior Distribution***
*Run the monte carlo

forvalues i = 2(1)901 {

*Print out an iteration number
display `i'

* put democracy index on 0-1 scale
foreach x of varlist mpi_v`i'_stock_10 {
quietly mi xeq: sum `x'
quietly mi xeq: replace `x' = (((`x' - `r(min)') * (1 - 0)) / (`r(max)' - `r(min)'))
}
*

*Fit the model, using the ith draw from the UDS posterior
set more off
preserve
*qui mibeta F.infmor_gapminder_ln mpi_v`i'_stock_10 e_migdppc_ln i.year i.country_id, vce(cluster country_id)
*qui local rsquared=e(r2_mi)
qui mi estimate, post: xtreg F.infmor_gapminder_ln mpi_v`i'_stock_10 e_migdppc_ln i.year, fe vce(cluster country_id)
qui outreg2 using Table_2_Model_8.dta, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle("`i'") addstat("Years", e(g_max_mi), "Countries", e(N_g_mi), "Observations", e(N_mi)) dta append


*Extract the coefficients and variance-covariance matrix
matrix b = e(b)
matrix V = e(V)
local blength = colsof(b)

*Preserve the dataset, take a single multivariate normal draw from the
*posterior distribution of the coefficients, and restore the dataset.
*We use the capture command to catch possible errors in drawnorm
*and drop these iterations gracefully.
*preserve 
capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
if _rc==0 {
mkmat b1-b`blength', matrix(bsample)
matrix posterior = nullmat(posterior) \ bsample
}
else {
display "Error drawing sample...iteration dropped"
}
restore

*Closes the Monte Carlo Loop
}

*Get posterior ready to work with
svmat posterior

*Calculate means and standard deviations
*tabstat posterior*, stat(mean sd)
keep posterior*
quietly outreg2 using Table_2_Model_8, eqkeep(mean sd) bdec(3) replace sum(log) excel
